import 'package:cloud_medicine/components/shop_picture.dart';
import 'package:cloud_medicine/http/config.dart';
import 'package:cloud_medicine/http/shop_api.dart';
import 'package:cloud_medicine/model/base_user_info.dart';
import 'package:cloud_medicine/model/shop_list.dart';
import 'package:cloud_medicine/pages/shop_detail.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:left_scroll_actions/leftScroll.dart';

class MyCollect extends StatefulWidget {
  MyCollect({Key key}) : super(key: key);

  @override
  _MyCollectState createState() => _MyCollectState();
}

class _MyCollectState extends State<MyCollect> {
  List<ShopItem> collectList = [];
  bool _loading = false;
  Future<Null> _init() async {
    setState(() {
      _loading = true;
    });
    dynamic res = await getCollectList_api('$backIP/shop/getCollect'); //接口路径
    res['data'].forEach((dynamic shopItem) {
      ShopItem shopItemModel = ShopItem.init(shopItem);
      collectList.add(shopItemModel);
    });
    print('~~~~~~~~~~');
    collectList.map((e) => print(e.name)).toList();
    if (mounted) {
      setState(() {
        _loading = false;
      });
    }
    return;
  }

  Future _collectOrCancel(num id) async {
    await collectOrCancel_api('$backIP/shop/collectShop', {'shopID': id});
  }

  @override
  void initState() {
    super.initState();
    _init();
  }

  Widget _childLayout() {
    if (user == null) {
      Fluttertoast.showToast(
        msg: '请先登录',
        backgroundColor: Colors.black,
        textColor: Colors.white,
      );
      return null;
    } else if (_loading) {
      return Center(
        child: Container(
          child: CircularProgressIndicator(),
        ),
      );
    } else {
      if (collectList.isEmpty) {
        return Center(
          child: Container(
            child: Text('还没有收藏哦~'),
          ),
        );
      } else {
        return ListView.builder(
            itemCount: collectList.length,
            itemBuilder: (context, index) {
              return LeftScroll(
                  buttons: [
                    LeftScrollItem(
                      text: '删除',
                      color: Colors.red,
                      onTap: () {
                        _collectOrCancel(collectList[index].id);
                        setState(() {
                          collectList.removeAt(index);
                          print(index);
                        });
                      },
                    ),
                  ],
                  child: Material(
                    color: Colors.transparent,
                    borderRadius: BorderRadius.circular(12),
                    child: InkWell(
                      splashColor: Colors.yellow.withOpacity(0.3),
                      highlightColor: Colors.yellow.withOpacity(0.1),
                      onTap: () {
                        Navigator.of(context).push(MaterialPageRoute(
                            builder: (context) => ShopDetail(
                                  shopid: collectList[index].id,
                                )));
                      },
                      child: Container(
                        color: Colors.white,
                        padding: EdgeInsets.all(10),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.start,
                          children: <Widget>[
                            Container(
                              child: ShopImg(
                                  imageStyle: 70,
                                  imgUrl: collectList[index].image),
                            ),
                            SizedBox(
                              width: 10,
                            ),
                            Expanded(
                                flex: 2,
                                child: Column(
                                  mainAxisAlignment: MainAxisAlignment.end,
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: <Widget>[
                                    Container(
                                      child: Text(
                                        collectList[index].name,
                                        style: TextStyle(
                                            fontSize: 21, fontFamily: 'main'),
                                      ),
                                    ),
                                    Container(
                                      padding: EdgeInsets.only(top: 10),
                                      child: Text(
                                          '评分：${collectList[index].grade}'),
                                    )
                                  ],
                                )),
                            Column(
                              children: <Widget>[
                                Text('配送￥${collectList[index].deliveryFee}'),
                                SizedBox(
                                  height: 10,
                                ),
                                Text('起送￥${collectList[index].iniDeliveryFee}')
                              ],
                            )
                          ],
                        ),
                      ),
                    ),
                  ));
            });
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('我的收藏'),
        ),
        body: _childLayout(),
        floatingActionButton: Align(
          alignment: Alignment.bottomCenter,
          child: Container(
            child: Text(
              '滑动可删除~',
              style: TextStyle(color: Colors.grey[400]),
            ),
          ),
        ));
  }
}
